package com.byxy.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.byxy.entity.Forum;
import com.byxy.entity.Reply;
import com.byxy.entity.Tab;
import com.byxy.entity.Tip;
import com.byxy.entity.User;
import com.byxy.service.ForumService;
import com.byxy.service.ReplyService;
import com.byxy.service.TabService;
import com.byxy.service.TipService;
import com.byxy.vo.TipVO;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class TipController {
	@Resource
	private ForumService forumService;
	
	@Autowired
	private TabService tabService;
	
	@Autowired
	private TipService tipService;
	
	@Autowired
	private ReplyService replyService;
	
	
	@GetMapping("toMainPage.do")
	public String tipFindAll(Model model) {
		model.addAttribute("tips",tipService.find());
		return "main";// ���ص����main��ת��main.jsp
	}
	
	
//	@RequiresPermissions(value= {"user:d","user:b"},logical = Logical.OR)
	@GetMapping("toPublishTipPage.do")
	public String add() {
		Subject subject = SecurityUtils.getSubject();
		subject.checkPermission("user:d");
		return "publishTip";
	}
	
	@PostMapping("publishNewTip.do")
	public String doAdd(Tip tip,String selectedForumId,HttpSession session,Model model) {
		List<Forum> forums = forumService.findAll();
		model.addAttribute("forums", forums);
		
		List<Tab> tabs = tabService.findAll();
		model.addAttribute("tabs", tabs);
		
		log.info("TipTitle:"+tip.getTipTitle());
		log.info("TipContent:"+tip.getTipContent());
//		log.info("TabId:"+tip.setTap.getTabId());
		log.info("selectedForumId:"+ selectedForumId);
		User u = (User)session.getAttribute("USER");
		tip.setUser(u);
		tip.setTipClick(0);
		tip.setTipIsdeleted(0);
		tip.setTipIsKnot(0);
		tip.setTipModifyTime(new Date());
		tip.setTipPublishTime(new Date());
		tip.setTipReplies(0);
		tipService.add(tip);
		return "redirect:toMainPage.do";
	}
	
	@GetMapping("showTip.do")
	public String showTip(Integer tipId,Model model,HttpSession session) {
		log.info("tipId:"+tipId);
		Integer tipId1 = (Integer) session.getAttribute("tipId");
		if(tipId != null) {
			TipVO tipVO = tipService.findById(tipId);
			model.addAttribute("tip", tipVO);
			List<Reply> replies = replyService.findById(tipId);
			model.addAttribute("replies", replies);
		}else {
			TipVO tipVO = tipService.findById(tipId1);
			model.addAttribute("tip", tipVO);
			List<Reply> replies = replyService.findById(tipId1);
			model.addAttribute("replies", replies);
		}
		return "tipContent";
	}
	
	
	@GetMapping("userKnotTip.do")
	public String knotTip(Integer tipId) {
		tipService.knotTip(tipId);
		return "redirect:toMainPage.do";
	}
	@GetMapping("toUserModifyTipPage.do")
	public String modifyTip() {
		return "modifyTip.jsp";
	}
}
